package com.lmb.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lmb.bean.Category;
import com.lmb.bean.Dish;
import com.lmb.bean.DishFlavor;
import com.lmb.bean.R;
import com.lmb.dto.DishDto;
import com.lmb.service.CategoryService;
import com.lmb.service.DishFlavorService;
import com.lmb.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @projectName: reggie
 * @package: com.lmb.controller
 * @className: DishController
 * @author: Eric
 * @description: TODO
 * @date: 2022/9/18 19:09
 */
@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;


    @Autowired
    private CategoryService categoryService;

    @GetMapping("/page")
    public R dishList( Integer page, Integer pageSize, String name){
        Page<Dish> dishPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.like(name!= null&&name.length()>0,Dish::getName,name);

        dishService.page(dishPage,dishLambdaQueryWrapper);

        Page<DishDto> dishDtoPage = new Page<>();
        BeanUtils.copyProperties(dishPage,dishDtoPage,"records");
        List<Dish> dishList = dishPage.getRecords();
        List<DishDto> dishDtoList = new ArrayList<>();
        dishList.stream().forEach(dish -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish,dishDto);
            Category category = categoryService.getById(dish.getCategoryId());
            dishDto.setCategoryName(category.getName());
            dishDtoList.add(dishDto);
        });

        dishDtoPage.setRecords(dishDtoList);
        return R.success(dishDtoPage);
    }

    @PostMapping
    public R addDish(@RequestBody DishDto dishDto ,HttpSession session){
        System.out.println(dishDto);
        return dishService.addDishAndDishFlavor(dishDto, session)?
                R.success("添加成功") :  R.error("添加失败");
    }

    @PostMapping("/status")
    public R upStatus(Long[] ids , Integer status ,HttpSession session){

        dishService.upStatuss(ids,status,session);

        return R.success("状态修改成功");

    }

    @DeleteMapping
    public R deleteDish(Long[] ids){

        dishService.deleteDishs(ids);

        return R.success("删除成功！！！");
    }

    @GetMapping("/{id}")
    public R DishById(@PathVariable Long id){

        DishDto dishDto = dishService.DishById(id);

        return R.success(dishDto);
    }

    @PutMapping
    public R upDish(@RequestBody DishDto dishDto,HttpSession session){
        dishService.upDish(dishDto,session);
        return R.success("修改菜品成功");
    }

    @GetMapping("/list")
    public R List(Long categoryId){

        List<DishDto> dishList = dishService.dishAndDishFlavorList(categoryId);

        return R.success(dishList);
    }


}
